﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Shop_of_Cl.Logic;

namespace Shop_of_Cl
{
    public partial class Form1 : Form
    {
        Main Shop;
        public Form1()
        {
            InitializeComponent();
            Shop = new Main();
            Shop.LoadData();
            SelectAll();
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            try
            {
                listBox1.Items.Clear();
                listBox1.Items.Add(Shop.ViewThing(Convert.ToInt32(textBox1.Text)));
            }
            catch (Exception e1)
            {
                listBox1.Items.Add("Введите число");
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {


        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            Shop.SaveData();
        }

        private void tabPage1_Enter(object sender, EventArgs e)
        {
            SelectAll();
        }

        void SelectAll()
        {
            listBox2.Items.Clear();
            listBox2.Items.Clear();
            for (int i = 0; i < Shop.CoutThingh(); i++)
            {
                listBox1.Items.Add(Shop.ViewThing(i));
            }
            for (int i = 0; i < Shop.CountCategories(); i++)
            {
                listBox2.Items.Add(Shop.ViewCategories(i));
            }
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            Shop.SaveData();
        }

        private void tabControl1_Enter(object sender, EventArgs e)
        {
            SelectAll();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (textBox2.Text.Length > 0)
            {
                Shop.AddCategories(textBox2.Text);
                Shop.SaveData();
                textBox2.Text = "";
                SelectAll();
            }
        }

        private void listBox2_MouseClick(object sender, MouseEventArgs e)
        {
            try
            {
                textBox2.Text = listBox2.Items[listBox2.SelectedIndex].ToString();
            }
            catch (ArgumentOutOfRangeException e1)
            { }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (textBox2.Text.Length > 0)
            {
                Shop.DeleteCategorie(textBox2.Text);
                textBox2.Text = "";
                Shop.SaveData();
                SelectAll();
            }
        }



     }
}
